package com.utils
{
	import flash.utils.getTimer;
	
	public class Lang
	{
		private static var lang:Lang;
		private static const CLIENT_LANG:String = 'ClientLang';
		private static const PREFIX_TAG:String = '{!';
		private static const SUFFIX_TAG:String = '}';
		
		private var _clientLangList:Array;
		
		public static var language:String = 'English';
		private var _langKinds:Array = [];
		
		
		public function Lang()
		{
			if(lang){
				throw Error('Lang不能重复创建实例!')
			}else{
				lang = this;
			}
			parsingResources();
			getLangKinds();
			trace('--->创建Lang实例!')
		}

		public function parsingResources():void{
			_clientLangList = Design.instance().load(CLIENT_LANG);
			for (var i:int = 0; i < _clientLangList.length; i++) 
			{
				var key:String = _clientLangList[i].id;
				_clientLangList[key] = _clientLangList[i]['lang<'+ language + '>'];
			}
		}
		
		private function getLangKinds():void{
			var temp:Object = _clientLangList[0];
			for(var key:String in temp){
				if(key != 'id'){
					_langKinds.push((key.split('lang<')[1]).split('>')[0]);
				}
			}
			_langKinds.sort(Array.DESCENDING);
		}
		
		public static function instance():Lang{
			if(!lang) lang = new Lang();
			return lang;
		}
		
		public function client(key:String,replacePairs:Object=null):String{
			var langStr:String = _clientLangList[key];
			if(!langStr) return key;
			for(var key:String in replacePairs){
				langStr = langStr.replace(new RegExp(PREFIX_TAG + key + SUFFIX_TAG,'g'),replacePairs[key]);
			}
			return htmlTxt(langStr);
		}
		
		private function htmlTxt(source:String):String{
			source = "<font face = 'Verdana'>" + source + "</font>"
			return source;
		}
		
		public function get langKinds():Array{return _langKinds;}
		
	}
}